using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using UnityEngine;

[CreateAssetMenu(menuName = "SOInstance/Data/WeaponData/Umbrella")]
public class UmbrellaDataSO : WeaponDataSO
{
    public GameObject area1;
    public GameObject area3;
    public GameObject area4;
    public GameObject bullet5;

    public int cout5 = 0;

    protected override void OnEnable()
    {
        base.OnEnable();

        cd = new List<float>() { 1f, 3f, 3f, 4f, 4f, 5f };
        skillAreaData = new List<SkillAreaData>()
        {
            new (false, true, false, 5.0f, SkillMode.Directed, SkillAreaType.Sector60, 5, 5, true, false, 1),

            new (false,true,false, 8,SkillMode.Directed, SkillAreaType.Sector60, 8, 8, true, false, 1),
            new (true, true,false, 0, SkillMode.Area, SkillAreaType.Circle, 1, 1, false, false, 1),
            new (true, true, true, 0, SkillMode.Area, SkillAreaType.Circle, 8, 8, false, false, 1),
            new (true, true, true, 0, SkillMode.Area, SkillAreaType.Circle, 12, 12, false, false, 1),
            new (true, true,false,22, SkillMode.Directed, SkillAreaType.RectArrow, 22, 10, false, false, 1),
        };
    }

    public override IEnumerator Skill0(SkillData info)
    {
        Vector2 mousePos2D = info.MousePos;
        Player player = owner as Player;
        var hits = Util.GetObjsSortedByDistance(Util.FindNearbyTarget(
            mousePos2D, player.controller.detectRange, player.controller.AttackLayers), mousePos2D);
        var tar = hits.Select(obj => obj.GetComponent<Damageable>())
        .FirstOrDefault(c => c != null && c.teamType != owner.teamType);
        if (tar && tar.teamType != owner.teamType
                    && Util.IsInDistance(tar.transform.position, owner.transform.position, skillAreaData[0].range))
        {
            info.DamageType = 0;
            info.Damage = owner.Stats[STATS.Attack].Value();

            owner.ApplyDamage(tar, info, true);

            owner.StartSkillCD(0);
            player.controller.ClearPath();
            yield break;
        }
        yield return null;
        owner.RefreshSkill(0);
    }

    public override IEnumerator Skill1(SkillData info)
    {
        info.DamageType = 1;
        info.Damage = owner.Stats[STATS.Power].Value();

        FreeList.Instance.GetObject(area1.name)
            .GetComponent<UmbrellaSkill1Area>().Initialize(info, skillAreaData[1].dimR);

        yield return new WaitForSeconds(0.1f);

        owner.StartSkillCD(1);
        yield return null;
    }

    public override IEnumerator Skill2(SkillData info)
    {

        owner.StartSkillCD(2);

        owner.Stats[STATS.Power].AddModifier(
            new TimedStatModifier(1, 3, (baseValue) => baseValue * 1.2f));

        yield return null;
    }

    public override IEnumerator Skill3(SkillData info)
    {

        owner.StartSkillCD(3);
        info.DamageType = 1;
        info.Damage = owner.Stats[STATS.Power].Value();

        FreeList.Instance.GetObject(area3.name)
            .GetComponent<UmbrellaSkill3Area>().Initialize(info, skillAreaData[3].dimR);

        yield return null;
    }

    public override IEnumerator Skill4(SkillData info)
    {
        info.DamageType = 1;
        info.Damage = owner.Stats[STATS.Power].Value();

        FreeList.Instance.GetObject(area4.name)
            .GetComponent<UmbrellaSkill4Area>().Initialize(info, skillAreaData[4].dimR);

        new Buff_Chanting(3f).Apply(owner, owner);
        yield return new WaitForSeconds(3f);

        owner.StartSkillCD(4);
        yield return null;
    }

    public override IEnumerator Skill5(SkillData info)
    {
        cout5 = 0;

        info.DamageType = 1;
        info.Damage = owner.Stats[STATS.Power].Value();

        yield return new WaitForSeconds(0.25f);
        for (int i = 0; i < 2; i++)
        {
            FreeList.Instance.GetObject(bullet5.name)
                .GetComponent<UmbrellaSkill5Bullet>().Init(info, Mathf.Clamp(cout5, 0, 10));
            yield return new WaitForSeconds(
                bullet5.GetComponent<UmbrellaSkill5Bullet>().intervalTime);
        }

        yield return new WaitForSeconds(1f);
        FreeList.Instance.GetObject(bullet5.name)
            .GetComponent<UmbrellaSkill5Bullet>().Init(info, Mathf.Clamp(cout5, 0, 10) * 2);

        owner.StartSkillCD(5);
        yield break;
    }
}
